#define _CRT_SECURE_NO_WARNINGS
#include <iostream>

using namespace std;
typedef long long LL;
const int MOD = 1e9 + 7, N = 1e6 + 10;
int n, m;
LL d[N], g[N], f[N];
LL qpow(LL a, LL b, LL p)
{
	LL ret = 1;
	while (b)
	{
		if (b & 1) ret = ret * a % p;
		a = a * a % p;
		b >>= 1;
	}
	return ret;
}
void init()
{
	int n = 1e6;
	f[0] = 1;
	for (int i = 1; i <= n; i++) f[i] = f[i - 1] * i % MOD;
	
	g[n] = qpow(f[n], MOD - 2, MOD);
	for (int i = n - 1; i >= 0; i--) g[i] = g[i + 1] * (i + 1) %MOD;
	d[2] = 1;
	for (int i = 3; i <= N; i++)
		d[i] = ((i - 1) * ((d[i - 1] + d[i - 2]) %
			MOD)) % MOD;
}


LL C(int n, int m)
{
	if (n < m) return 0;
	return f[n] * g[m] % MOD * g[n - m] % MOD;
}
int main()
{
	init();

	int t; cin >> t;
	
	while (t--)
	{
		cin >> n >> m;
		int k = n - m;
		if (k < 0) cout << 0 << endl;
		else if (k == 0) cout << 1 << endl;
		else cout << C(n,m) * d[k] % MOD << endl;
	}
	return 0;
}